/*
 *https://leetcode.cn/problems/edit-distance/
 *lc72. 编辑距离
 *medium 吴朝泽 2024.12.6
 *动态规划
*/

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
    int minDistance(string word1, string word2) {
        int m = word1.size(), n = word2.size();
        int f[m + 1][n + 1];
        for (int j = 0; j <= n; ++j) 
            f[0][j] = j;
        
        for (int i = 1; i <= m; ++i) 
        {
            f[i][0] = i;
            for (int j = 1; j <= n; ++j)
             {
                if (word1[i - 1] == word2[j - 1])
                    f[i][j] = f[i - 1][j - 1];
                else 
                    f[i][j] = min({f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]}) + 1;
            }
        }
        return f[m][n];
    }
};